package com.goods.controller.business;

import com.github.pagehelper.PageInfo;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Hachimen
 * @date ：Created in 2021/9/24 11:03
 * @description：类别列表
 * @modified By：
 * @version:
 */
@RestController
@RequestMapping("/business/productCategory")
public class ProductCategoryWebController {

    @Autowired
    private ProductCategoryService productCategoryService;

    //物资类别 分页
    //@RequestParam String pageNum, @RequestParam String pageSize
    @GetMapping("/categoryTree")
    public ResponseBean categoryTree(HttpServletRequest request){
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");

        // http://www.localhost:8989/business/productCategory/categoryTree?pageNum=1&pageSize=5
        PageInfo<ProductCategoryTreeNodeVO> page = productCategoryService.categoryTree(pageNum,pageSize);
        Map<String, Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",page.getList());
        return ResponseBean.success(data);
    }

    //物资类别 查询父级分类
    @GetMapping("/getParentCategoryTree")
    public ResponseBean getParentCategoryTree(){
        List<ProductCategoryTreeNodeVO> ProductCategoryTreeNodeVOList = productCategoryService.getParentCategoryTree();
        return ResponseBean.success(ProductCategoryTreeNodeVOList);
    }

    //物资分类 添加
    @PostMapping("/add")
    public ResponseBean add(@RequestBody ProductCategoryVO productCategoryVO){
        productCategoryService.addProductCategory(productCategoryVO);
        return ResponseBean.success();
    }

    //物资分类 修改之 查单个
    //http://www.localhost:8989/business/productCategory/edit/33
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        //ProductCategoryVO productCategoryVo = productCategoryService.edit(id);
        ProductCategory productCategory = productCategoryService.edit(id);
        return ResponseBean.success(productCategory);
    }

    //物资分类 修改之 保存单个
    @PutMapping("/update/{id}")
    public ResponseBean update(@RequestBody ProductCategory productCategory){
        productCategoryService.update(productCategory);
        return ResponseBean.success();
    }

    //物资分类 删除
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        productCategoryService.delete(id);
        return ResponseBean.success();
    }
}
